log = require('../utils/log').getLogger 'admin_activity'
db = require '../utils/db'

module.exports = 
	'get /admin/activity': (req, res)->
		sql = "select * from activity"
		db.runSql sql, (err, result)->
			log.logError err
			res.send result

	'post /admin/activity': (req, res)->
		sql = "insert into activity set ?"
		req.body.start_time = if req.body.start_time then new Date(req.body.start_time) else new Date()
		req.body.end_time = if req.body.end_time then new Date(req.body.end_time) else Date()
		db.runSql sql, [req.body], (err, result)->
			log.logError err
			res.send success: not err?

	'get /admin/activity/:id': (req, res)->
		sql = "select * from activity where id = ?"
		db.queryOne sql, [req.params.id], (err, result)->
			log.logError err
			res.send result

	'post /admin/activity/:id': (req, res)->
		sql = "update activity set ? where id = ?"
		req.body.create_time = new Date(req.body.create_time)
		req.body.update_time = new Date()
		req.body.start_time = new Date(req.body.start_time)
		req.body.end_time = new Date(req.body.end_time)
		db.runSql sql, [req.body,req.params.id], (err, result)->
			log.logError err
			res.send success: not err?

	'get /admin/activity/sign/all': (req, res)->
		sql = "select s.*,a.name,a.id from activity_sign as s join activity as a on s.activity_id = a.id "
		db.runSql sql, (err, result)->
			log.logError err
			res.send result

	'get /admin/activity/:id/sign': (req, res)->
		sql = "select s.*,a.name,a.id from activity_sign as s join activity as a on s.activity_id = a.id where a.id = ?"

		db.runSql sql, [req.params.id],(err, result)->
			log.logError err
			res.send result


	'del /admin/activity/:id': (req, res)->
		sql = "delete from activity where id = ?"
		db.runSql sql, [req.params.id], (err, result)->
			log.logError err
			res.send success: not err?